package fun.ty13.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import fun.ty13.domain.Order;
import fun.ty13.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 桃缘十三
 * @date 2022/11/25 15:50
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "userFallback")
    public Order get(@PathVariable Long id) {
        String url = "http://user-server/user/" + id;
        User user = restTemplate.getForObject(url, User.class);
        return new Order(id, "66666666", user);
    }

    public Order userFallback(Long id) {
        return new Order(id, "网络拥堵，请稍后再试", null);
    }
}
